﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.IO.Ports;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;

namespace FreedomKeyboard
{

    class Keyboard
    {
        [DllImport("coredll.dll")]
        static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
        //public event ParseKey KeyPress_Event;
        //public delegate void ParseKey(string key);
        //private char k1;
        //private char k2;
        SortedList keys = new SortedList();
        private bool shift = false;
        //private bool rshift = false;
        public Keyboard()
        {
            //Keys ky = new Keys();
            //ky = (Keys)Enum.Parse(ky.GetType(), "a", false);
            
            StreamReader sr = new StreamReader("\\keys.txt");
            
            string strLine;
            string[] tmp;
            while ((strLine = sr.ReadLine()) != null)
            {
                try
                {
                    tmp = strLine.Split(':');
                    keys.Add(tmp[0], Int32.Parse(tmp[1]));
                }
                catch (Exception e)
                {

                }
            }
            //keys.
            //this.ser = sp;
            //this.ser.DataReceived += new SerialDataReceivedEventHandler(this.SerialEvent);
        }
        public void Key(char key)
        {
            const int KEYEVENTF_KEYPRESS = 0x0000;
            const int KEYEVENTF_KEYUP = 0x0002;
            const int VK_LSHIFT = 0xA0;
            const int VK_RSHIFT = 0xA1;
            if ((int)key == 59 || (int)key == 60)
            {
                shift = true;
                return;
            }
            if ((int)key == 122 || (int)key == 123)
            {
                shift = false;
                return;
            }
            if ((int)key == 95)
            {
                keybd_event((byte)Keys.Back, 0, KEYEVENTF_KEYPRESS, 0);
                keybd_event((byte)Keys.Back, 0, KEYEVENTF_KEYUP, 0);
            }
            if ((int)key == 85 || (int)key == 116)
            {
                keybd_event((byte)Keys.Space, 0, KEYEVENTF_KEYPRESS, 0);
                keybd_event((byte)Keys.Space, 0, KEYEVENTF_KEYUP, 0);
            }
            Keys ky = new Keys();
            //Keys.spa
            string keyx;
            if (this.keys.ContainsValue((int)key))
            {
                IDictionaryEnumerator _enumerator = keys.GetEnumerator();
                while (_enumerator.MoveNext())
                {
                    if ((int)_enumerator.Value == (int)key)
                    {
                        keyx = (string)_enumerator.Key;
                        try
                        {
                            ky = (Keys)Enum.Parse(ky.GetType(), keyx.ToUpper(), false);
                        }
                        catch (Exception e)
                        {
                            ky = (Keys)Enum.Parse(ky.GetType(), keyx, false);
                        }
                        
                            
                       
                        if (shift)
                            keybd_event(VK_LSHIFT, 0, KEYEVENTF_KEYPRESS, 0);
                        keybd_event((byte)ky, 0, KEYEVENTF_KEYPRESS, 0);
                        keybd_event((byte)ky, 0, KEYEVENTF_KEYUP, 0);
                        if (shift)
                            keybd_event(VK_LSHIFT, 0, KEYEVENTF_KEYUP, 0);
                    }
                }
            }
        }

    }
}
